package com.cmb.homework0722;

import com.cmb.homework0722.exception.MyException;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Random;

/**
 * @Author llk
 * @Description //商店类，实现Runnable接口
 * @Date 21:31 2022/7/23
 **/
public class StoreThread implements Runnable {
    // 将买商品的用户作为属性
    private Customer customer;
    // 将商店内的所有商品集合作为属性
    private static HashMap<Integer, Goods> goodsMap;
    // 用户到店购买商品的行为次数
    private static Integer purchaseFrequency = 0;

    static {
        goodsMap = new HashMap<>();
        Goods mask = new Goods(1, "mask", new BigDecimal("1"), 1000);
        goodsMap.put(mask.getId(), mask);
        Goods water = new Goods(2, "water", new BigDecimal("3"), 2000);
        goodsMap.put(water.getId(), water);
        Goods cola = new Goods(3, "cola", new BigDecimal("5"), 3000);
        goodsMap.put(cola.getId(), cola);
    }

    public StoreThread(Customer customer) {
        this.customer = customer;
    }

    public StoreThread() {
    }

    public static HashMap<Integer, Goods> getGoodsMap() {
        return goodsMap;
    }

    public static Integer getPurchaseFrequency() {
        return purchaseFrequency;
    }

    public static int[] getGoodsIdArr() {
        int[] goodsIdArr = new int[goodsMap.size()];
        int i = 0;
        for (Integer id : goodsMap.keySet()
        ) {
            goodsIdArr[i] = id;
            i++;
        }
        return goodsIdArr;
    }

    /**
     * @return java.lang.Integer
     * @Author llk
     * @Description // 获取商店内的商品个数
     * @Date 21:29 2022/7/23
     * @Param []
     **/
    public static Integer getMapValueSum() {
        int sum = 0;
        for (Goods ele :
                goodsMap.values()) {
            sum += ele.getCounts();
        }
        return sum;
    }

    @Override
    public void run() {

        int[] goodsIdArr = getGoodsIdArr();
        while (StoreThread.getPurchaseFrequency()<50000) {
            synchronized (String.class) {
                purchaseFrequency++;
                // 首先判断是否有积分，没有积分直接结束
                if (customer.getCredit().compareTo(new BigDecimal("0")) != 1) {
//                    try {
//                        throw new MyException("用户：" + customer.getName() + "没有积分了!");
//                    } catch (MyException e) {
//                        e.printStackTrace();
//                        break;
//                    }
                    System.out.println("meiyoujifen");
                    return;
                }
                // 随机购买商品
                int randomGoodsIdIndex = new Random().nextInt(goodsIdArr.length);
                int randomGoodsId = goodsIdArr[randomGoodsIdIndex];
                // 判断手中的积分余额是否小于需要购买的商品的积分 并且商品是否还有库存
                Goods buyGoods = goodsMap.get(randomGoodsId);
                if (customer.getCredit().compareTo(goodsMap.get(randomGoodsId).getGoodsCredit()) == -1) {
                    System.out.println("用户：" + customer.getName() + "购买" + buyGoods.getGoodsName() + "所需要的的积分不够了");
                    continue;
                }

                if (buyGoods.getCounts() <= 0) {
                    System.out.println("用户：" + customer.getName() + "想要购买的" + buyGoods.getGoodsName() + "卖完了");
                    continue;
                }
                // 完成购买商品的动作
                buyGoods.setCounts(buyGoods.getCounts() - 1);
                customer.setCredit(customer.getCredit().subtract(buyGoods.getGoodsCredit()));
                HashMap<String, Integer> userGoodsMap = customer.getGoodsMap();
                if (userGoodsMap.containsKey(buyGoods.getGoodsName())) {
                    userGoodsMap.put(buyGoods.getGoodsName(), userGoodsMap.get(buyGoods.getGoodsName()) + 1);
                } else {
                    userGoodsMap.put(buyGoods.getGoodsName(), 1);
                }
            }

        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
}
